<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Home\Controller;
use Think\Controller;

class JgshowController extends BaseController {

	//报名页面
	public function entered(){
		$rootid = I('get.rootid');
		$isreg = $this->isreg($rootid);
		$this->assign('getURL',getURL());
		$this->assign('isreg',$this->isreg($rootid));
		$this->display();
	}

	//如果是金港赛车的页面则判断登陆注册信息状态
	protected function isreg($rootid=null){
		if($rootid==78){
			$apply = D(C('TABLE_APPLY'));
			$res = $apply->where(array('openid'=>$_COOKIE['user_userinfo']))->count();
			if($res){
				return 1;
			}else{
				return 0;
			}
		}else{
			return 0;
		}

	}

	//开奖倒计时页面（包括显示中奖名单信息）
	public function time(){
		//开奖次数
		$kjcishu = $this->getawardnum();
		//获取当前倒计时时间
		$getkjtime = $this->getkjtime($kjcishu);
		//获取中奖信息
		$awardinfo = $this->getawardinfo($kjcishu);
		$this->assign('awardinfo',$awardinfo);
		$this->assign('kjtime',$getkjtime);
		$this->display();
	}

	//获取第几次开奖
	protected function getawardnum(){
		$applys = D(C('TABLE_APPLYS'));
		$cishu = $applys->max('awarpnum');
		if(empty($cishu) || $cishu<1){
			return 0;
		}else{
			return $cishu;
		}
	}

	//获取开奖时间
	protected function getkjtime($kjcishu=0){
		$applytime = D(C('TABLE_APPLYTIME'));
		$res = $applytime->order('id asc')->select();
		if($res){
			return $res[$kjcishu]['awardtime'];
		}
	}

	//获取上一轮的中奖名单
	protected function getawardinfo($kjcishu=0){
		$applys = D(C('TABLE_APPLYS'));
		$res = $applys->where(array('awarpnum'=>$kjcishu))->select();
		return $res;
	}

	//用户访问页面及发送短信通知
	protected function sendduanxin(){
		$openid = $_COOKIE['user_userinfo'];
		$applys = D(C('TABLE_APPLYS'));
		$res = $applys->where(array('openid'=>$openid))->find();
		if($res){
			//先拿到手机号码
			$mobile = $res['mobile'];
		}
		//如果发送成功,则标记发送短信的状态
		if(true){
			$this->changesendstatus($openid);
		}else{
			//echo '短信发送失败';
		}
	}
	//改变发送中奖信息状态
	protected function changesendstatus($openid=0){
		$applys = D(C('TABLE_APPLYS'));
		$data['sendstatus'] = 1;
		$res = $applys->where(array('openid'=>$openid))->save($data);
		if($res){
			return true;
		}else{
			return false;
		}
	}

	//查询
	public function search(){
		$res = $this->getallmd();
		$this->assign('result',$res);
		$this->display();
	}

	//获取所有中奖名单
	protected function getallmd(){
		$applys = D(C('TABLE_APPLYS'));
		$res = $applys->select();
		return $res;
	}

	public function searchs(){
		$mobile = I('post.mobile');
		if(!empty($mobile)){
			$applys = D(C('TABLE_APPLYS'));
			$res = $applys->where(array('mobile'=>$mobile))->find();
			if($res){
				$result['ret'] = $this->getawardinfo1($res['awardid'],"awardname");;
			}else{
				$result['ret'] = 0;
			}
			echo json_encode($result);
		}
	}

	//根据奖品ID查询奖品信息
	protected function getawardinfo1($id,$field){
		$award = D(C('TABLE_AWARD'));
		$res = $award->where(array('id'=>$id))->find();
		if($res){
			return $res[$field];
		}
	}

}